/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package azhar.engine;

import azhar.view.Main;
import java.util.ArrayList;
import java.util.List;
import org.smslib.*;
import org.smslib.AGateway.Protocols;
import org.smslib.InboundMessage.*;
import org.smslib.modem.*;

/**
 *
 * @author Al-Insan
 */
public class MessageService {

    private List<InboundMessage> msgList;
    private SerialModemGateway gateway;
    private OutboundMessage send;
    private InboundNotification inbound;
    private OrphanedMessageNotification orphan;
    private GatewayStatusNotification gatStatus;

    public MessageService(String port, Main m) throws Exception {
        System.out.println("Example: Read messages from a serial gsm modem.");
        System.out.println(Library.getLibraryDescription());
        System.out.println("Version: " + Library.getLibraryVersion());
        System.getProperty("com.sun.comm.Win32Driver");
        gateway = new SerialModemGateway("modem.com1", port, 115200, "Huawei", "E160");
        inbound = new InboundNotification(m);
        orphan = new OrphanedMessageNotification(m);
        gatStatus = new GatewayStatusNotification(m);
        initGateway();
    }

    public void initGateway() throws Exception {
        gateway.setProtocol(Protocols.PDU);
        gateway.setInbound(true);
        gateway.setOutbound(true);
        gateway.setSimPin("0000");
        gateway.setSmscNumber("");
        Service.getInstance().setOutboundMessageNotification(new OutboundNotification());
        Service.getInstance().setInboundMessageNotification(inbound);
        Service.getInstance().setCallNotification(new CallNotification());
        Service.getInstance().setGatewayStatusNotification(gatStatus);
        Service.getInstance().setOrphanedMessageNotification(orphan);
        Service.getInstance().addGateway(gateway);
    }

    public void startService() throws Exception {
        Service.getInstance().startService();
    }

    public void restartService() throws Exception{
        Service.getInstance().startService();
    }

    public void stopService() throws Exception {
        Service.getInstance().stopService();
    }

    public List<InboundMessage> readMessage() throws Exception {
        msgList = new ArrayList<InboundMessage>();
        Service.getInstance().readMessages(msgList, MessageClasses.UNREAD);
        return msgList;
    }

    public void deleteMessage(InboundMessage msg) throws Exception{
        Service.getInstance().deleteMessage(msg);
    }

    public OutboundMessage sendMessage(String r, String m) throws Exception {
        send = new OutboundMessage(r, m);
        Service.getInstance().sendMessage(send);
        return send;
    }
    
    public SerialModemGateway getGateway(){
        return gateway;
    }

    public void printGatewayInfo() throws Exception {
        System.out.println("Modem Information:");
        System.out.println("  Manufacturer: " + gateway.getManufacturer());
        System.out.println("  Model: " + gateway.getModel());
        System.out.println("  Serial No: " + gateway.getSerialNo());
        System.out.println("  SIM IMSI: " + gateway.getImsi());
        System.out.println("  Signal Level: " + gateway.getSignalLevel() + " dBm");
        System.out.println("  Battery Level: " + gateway.getBatteryLevel() + "%");
        System.out.println();
    }

    public int getBatteryLevel() throws Exception{
        return gateway.getBatteryLevel();
    }

    public int getSignalLevel() throws Exception{
        return gateway.getSignalLevel();
    }

    public String getModemInfo() throws Exception{
        return gateway.getManufacturer()+" "+gateway.getModel();
    }
}
